﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XZL.GM.Dal;
using XZL.GM.Bll;


public partial class Course_BannerEdit : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindBannerList();
        }
    }


    /// <summary>
    /// 绑定Banner信息
    /// </summary>
    private void BindBannerList()
    {

    string ban = Request.QueryString["BannerID"];
    if( ban!=null )
    {
        int bannerID = Convert.ToInt32(ban);
        Proc_Banner_SelectInfoResult banner = BannerInfoBiz.GetBannerInfo(bannerID);
        if (banner != null)
        {
            txtBannerName.Text=banner.BannerName;
            txtBannerLink.Text=banner.BannerLink;
            txtSort.Text = Convert.ToString(banner.Sort);


            string imgPath=System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
            if (banner.BannerImg == "" || banner.BannerImg == null)
            {

            }
            else
            {
                imgSignature.ImageUrl = imgPath + banner.BannerImg;
                lblFileName.Text = banner.BannerImg;
            
            }

            if (banner.Status == 1)
            {
                rb1.Checked = true;
            }
            else
            {
                rb2.Checked = true;
            }

        }

    }

 }

    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnOK_Click(object sender, EventArgs e)
    {

        string str=Request.QueryString["BannerID"];

        if (txtBannerName.Text.Trim()=="")
        {
            lblWarn.Text = "请输入Banner昵称！";
            txtBannerName.Focus();
            return;
        }

        if (txtBannerLink.Text.Trim() == "")
        {
            lblWarn.Text = "请输入Banner链接！";
            txtBannerLink.Focus();
            return;
        }

        int sort = 0;
        try
        {
            sort = Convert.ToInt32(txtSort.Text.Trim());
            
        }
        catch (Exception ex)
        {
            lblWarn.Text = "请输入正确排序数字！";
            txtSort.Text = "0";
            txtSort.Focus();
            return;
        }


        int status = 1;
        if (rb2.Checked == true)
        {
            status = -1;
        }


        int bannerID = Convert.ToInt32(str);
        string bannerName = txtBannerName.Text.Trim();
        string bannerLink = txtBannerLink.Text.Trim();     
        string bannerImg = lblFileName.Text.Trim();

        Proc_BannerInfo_SelectListResult banner = new Proc_BannerInfo_SelectListResult()
        {
            BannerID=bannerID,
            BannerName = bannerName,
            BannerLink = bannerLink,
            BannerImg = bannerImg,
            Sort = sort,
            Status = status

        };

        //编辑首页信息
        if(banner!=null)
        {
            if (str != null)
            {
                banner.BannerID = Convert.ToInt32(str);
                bool d = BannerInfoBiz.BannerUpdate(bannerID, bannerName, bannerLink, bannerImg, sort, status);
                if (d)
                {
                    lblWarn.Text = "首页信息编辑成功！";
                    lblWarn.ForeColor = System.Drawing.Color.Green;
                    btnOK.Enabled = false;
                }
                else
                {

                    lblWarn.Text = "首页信息编辑失败！";
                    lblWarn.ForeColor = System.Drawing.Color.Red;

                }

                Response.Redirect("BannerList.aspx");
            }

                //添加首页信息
            else
            {
                bool d = BannerInfoBiz.BannerAddition(bannerName, bannerLink, bannerImg, sort,status);
                if(d) 
                {
                    lblWarn.Text = "首页信息添加成功！";
                    lblWarn.ForeColor = System.Drawing.Color.Green;
                    btnOK.Enabled = false;
                }
                else
                {

                    lblWarn.Text = "首页信息添加失败！";
                    lblWarn.ForeColor = System.Drawing.Color.Red;

                }
                Response.Redirect("BannerList.aspx");
            
            }
        }
    }


    /// <summary>
    /// 上传讲师图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        if (txtSelectFile.HasFile)
        {
            HttpPostedFile postedFile = txtSelectFile.PostedFile;
            string fileName = System.IO.Path.GetFileName(postedFile.FileName);
            if (fileName != "")
            {
                string fileExtension = System.IO.Path.GetExtension(fileName);
                string newName = Guid.NewGuid().ToString();
                string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
                string path = System.Web.HttpContext.Current.Request.MapPath(imgPath) + newName + fileExtension;
                lblFileName.Text = newName + fileExtension;
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意：可能要修改你的文件夹的匿名写入权限。
                postedFile.SaveAs(path);
                lblUploadInfo.Text = "上传成功";
                imgSignature.ImageUrl = imgPath + newName + fileExtension;
            }
        }
    }


    /// <summary>
    /// 返回
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("BannerList.aspx");
    }



}